﻿var ns = namespace("kaskelot.sp.ui");

ns.kurstillfalledetails = function () {

	function init() {
		$("#tab-container ul").hide();

		getKurstillfalle();
		getPlacering();
	}

	function getKurstillfalle() {
		var id = getParameterByName("KurstillfalleId");

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/GetKursTillfalle",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetKursTillfalleResult.HasError) {
						$("#kurstillfalle-details").html(
							$("#kurstillfalle-details-template").render(data.GetKursTillfalleResult.Data)
						);

						$("#andradav-container").html(
							$("#andradav-template").render(data.GetKursTillfalleResult.Data)
						);

						$("#title-container").html(data.GetKursTillfalleResult.Data.KursNamn);
					}
				},
			error:
				function (xhr) {
					//kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function getPlacering() {
		var id = getParameterByName("KurstillfalleId");

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/GetPersonKursTillfallenByKursTillfalleId",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.GetPersonKursTillfallenByKursTillfalleIdResult.HasError) {
						$("#deltagare-results tbody").html(
							$("#deltagare-template").render(data.GetPersonKursTillfallenByKursTillfalleIdResult.Data)
						);

						$("#deltagare-results tbody .chzn").chosen({ allow_single_deselect: false, disable_search: true });

						var x = data.GetPersonKursTillfallenByKursTillfalleIdResult.Data;
						for (var i = 0; i < x.length; i++) {
							if (x[i].PersonId == "")
								continue;
							
							$("#" + x[i].Id).val(x[i].Status).trigger("liszt:updated");
						}
					}
				},
			error:
				function (xhr) {
					//kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function changeStatus(pktId) {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/TogglePersonKurstillfalleStatus",
			type: "GET",
			data: { id: pktId },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.TogglePersonKurstillfalleStatusResult.HasError) {
						getPlacering();
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function updateKurstillfalle() {
		var id = getParameterByName("KurstillfalleId");

		var selector = "#kurstillfalle-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "KursTillfalleForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#kurstillfalle-form").dialog({ title: "Redigera kurstillfälle" });
				$(".chzn-select-deselect").chosen({ allow_single_deselect: true, disable_search: true });
				$("#antal-container").hide();
				$("#kursansvarig-search-input-container").show();
				$("#kursansvarig-toggle-search").show();

				$("#KursDatum").datepicker();;
				$("#Starttid").datetimepicker({ showButtonPanel: false, stepMinute: 5, timeOnly: true });
				$("#Sluttid").datetimepicker({ showButtonPanel: false, stepMinute: 5, timeOnly: true });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "UpdateKursTillfalle";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getKurstillfalle();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function deleteKurstillfalle() {
		var id = getParameterByName("KurstillfalleId");

		var selector = "#kurstillfalle-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "KursTillfalleForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#KursDatum").attr("disabled", "disabled");
				$("#KursId").attr("disabled", "disabled");
				$("#Sal").attr("disabled", "disabled");
				$("#Adress").attr("disabled", "disabled");
				$("#Lokal").attr("disabled", "disabled");
				$("#Starttid").attr("disabled", "disabled");
				$("#Sluttid").attr("disabled", "disabled");
				$("#antal-container").hide();
				$("#kursansvarig-search-input-container").hide();
				$("#kursansvarig-toggle-search").hide();

				$("#kurstillfalle-form").dialog({ title: "Ta bort kurstillfälle" });
				$(".chzn-select-deselect").chosen({ allow_single_deselect: true, disable_search: true });
				$("#KursDatum").datepicker();;
				$("#Starttid").datetimepicker({ showButtonPanel: false, stepMinute: 5, timeOnly: true });
				$("#Sluttid").datetimepicker({ showButtonPanel: false, stepMinute: 5, timeOnly: true });
			},
			okFunction: function () {
				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/DeleteKursTillfalle",
					type: "GET",
					data: { id: id },
					processData: true,
					cache: false,
					success:
						function (data) {
							if (!data.DeleteKursTillfalleResult.HasError) {
								window.location = "./default.aspx";
							}
							else {
								kaskelot.sp.ui.prompts.showErrorPrompt(data.DeleteKursTillfalleResult.ErrorMessage);
							}
						},
					error:
						function (xhr) {
							kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
						}
				});
			}
		});
	}

	function addPersonKurstillfalle() {
		var id = getParameterByName("KurstillfalleId");

		var selector = "#addpersonkurstillfalle-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "AddPersonKursTillfalleForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$(selector + " #Antal").spinner({ min: 1 });
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "AddPersonKursTillfalle";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getPlacering();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function deletePersonKurstillfalle(id) {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientKursService.svc/DeletePersonKursTillfalle",
			type: "GET",
			data: { id: id },
			processData: true,
			cache: false,
			success:
				function (data) {
					if (!data.DeletePersonKursTillfalleResult.HasError) {
						getPlacering();
					}
				},
			error:
				function (xhr) {
					kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
				}
		});
	}

	function getSelectedValue() {
		var val = [];
		$("#deltagare-results tr").each(function (i) {
			val[i] = $(this).attr("data-email");
		});

		return val;
	}

	function showEmail() {
		var val = getSelectedValue();

		var selector = "#epost-form";
		kaskelot.sp.ui.formutils.getAndShowPostForm({
			params: {
				formName: "EpostForm",
				idList: val,
			},
			selector: selector,
			openFunction: function () {
				$(".ui-dialog-buttonset .ui-button + .ui-button").hide();

				var doc = document;
				var text = doc.getElementById("Text");

				// Sätt text som markerad
				if (doc.body.createTextRange) { // ms
					var range = doc.body.createTextRange();
					range.moveToElementText(text);
					range.select();
				} else if (window.getSelection) { // moz, opera, webkit
					var selection = window.getSelection();
					var range = doc.createRange();
					range.selectNodeContents(text);
					selection.removeAllRanges();
					selection.addRange(range);
				}
			},
			okFunction: function () {
				$(selector).dialog("close");
			}
		});
	}

	return {
		init: init,
		getKurstillfalle: getKurstillfalle,
		getPlacering: getPlacering,
		updateKurstillfalle: updateKurstillfalle,
		deleteKurstillfalle: deleteKurstillfalle,
		addPersonKurstillfalle: addPersonKurstillfalle,
		deletePersonKurstillfalle: deletePersonKurstillfalle,
		showEmail: showEmail,
		changeStatus: changeStatus
	};
}();